/*
 * Hal_CAN.h
 *
 *  Created on: Jul 5, 2023
 *      Author: work
 */

#ifndef HAL_CAN_H_
#define HAL_CAN_H_


#include "can_lld.h"
#include "typedefs.h"
#include "siul_lld.h"
#include "can_demo.h"
#include "typedefs.h"

#define HAL_CAN_WAIT 10000
typedef enum
{
        CAN_CHANNEL0 = 0,
        CAN_CHANNEL1 = 1,
        CAN_CHANNEL2 = 2,
        CAN_CHANNEL3 = 3,
        CAN_CHANNEL4 = 4,
        CAN_CHANNEL5 = 5,
        CAN_CHANNEL6 = 6
}can_channel;

typedef enum {
  HAL_CAN_TRCV_MODE_NORMAL = 0,  
  HAL_CAN_TRCV_MODE_STANDBY,     
  HAL_CAN_TRCV_MODE_SLEEP        
} HAL_CAN_TRCV_ModeTypeDef;

typedef enum{
  CONTROLLERMODE_UNINIT = 0, 
  CONTROLLERMODE_STOPPED,
  CONTROLLERMODE_STARTED,
  CONTROLLERMODE_SLEEP,
  CONTROLLERMODE_BUSOFF,
  CONTROLLERMODE_BUTT
}ControllerModeType;

typedef enum{
  IRQ_TYPE_RX = 0, 
  IRQ_TYPE_BUSOFF,
  IRQ_TYPE_RX_BUSOFF,
  IRQ_TYPE_BUTT
}irq_type;

typedef enum
{
        CAN_DISABLE = 0,
        CAN_ENABLE  = 1
}can_enabled;
typedef enum
{
        TDC_DISABLE = 0,
        TDC_ENABLE  = 1
}TDC_enabled;

typedef enum
{
        ID_TYPE_STAND = 0,//标准ID 
        ID_TYPE_EXSTAND, //扩展ID 
        ID_TYPE_FD_STAND,
        ID_TYPE_FD_EXSTAND,
        ID_TYPE_BUTT
}ID_type;

typedef enum  {
    CANHAL_MBPS_1 = 0,
    CANHAL_KBPS_800,
    CANHAL_KBPS_500,
    CANHAL_KBPS_250,
    CANHAL_KBPS_125,
    CANHAL_KBPS_100,
    CANHAL_BIT_RATE_BUTT
}arbitr_baudrate;
typedef enum  {
    CANFD_MBPS_8 = 0,
    CANFD_MBPS_4,
    CANFD_MBPS_2,
    CANFD_MBPS_1,
    CANFD_KBPS_500,
    CANFD_KBPS_400,
    CANFD_KBPS_200,
    CANFD_KBPS_125,
    CANFD_KBPS_100,
    CANFD_BIT_RATE_BUTT
} data_baudrate;
typedef struct 
{
        uint8_t* buff[8];
        uint32_t ID;
        ID_type  id_type;
        uint8_t  DLC;
}pdu_str;

typedef struct 
{
        uint32_t filter_code;
        uint32_t filter_mask;
        ID_type id_type;
}filter_struct;

typedef struct 
{
        arbitr_baudrate arbitr;
        data_baudrate databitr;
        void (*filter_cbk)(filter_struct *filter_array,uint8_t len);
        can_enabled canenabled;
        uint8_t offset;
        TDC_enabled tdcenabled;
}str;

typedef void (*func)(void);
typedef void (*recv_func)(const pdu_str *);

uint32_t hal_can_init(can_channel channel,str * hal_str);	//hal_can_init(CAN_CHANNEL0);
void hal_can_enabled(can_channel channel,can_enabled is_enable);//hal_can_enabled(CAN_CHANNEL0,CAN_ENABLE);
void hal_can_baudrate(can_channel channel,CANBitRate baudrate);//hal_can_baudrate(CAN_CHANNEL0,CAN_KBPS_500);
void hal_can_tq(can_channel channel,uint8_t prop_seg,uint8_t seg1,uint8_t seg2,uint8_t sjw,uint8_t brp);//hal_can_tq();
void hal_can_filter_config(can_channel channel,filter_struct can_filter_array[],uint16_t len);//hal_can_filter_config
uint32_t hal_can_write(can_channel channel,pdu_str * pdu);//hal_can_write
uint32_t hal_can_read(can_channel channel,CANRxMsg *rx_msg);//hal_can_read
void hal_can_recv_cbk(can_channel channel ,recv_func recv_func_cbk);//
void hal_busoff_cbk(can_channel channel ,func func_cbk);//
void hal_can_trcv_mode(can_channel channel, HAL_CAN_TRCV_ModeTypeDef mode);//
HAL_CAN_TRCV_ModeTypeDef hal_can_get_trcv_mode(can_channel channel);//
void hal_can_ctrl_mode(can_channel channel,ControllerModeType ctrl_mode);//
ControllerModeType hal_can_get_ctrl_mode(can_channel channel);//
uint8_t hal_can_TEC(can_channel channel);//
uint8_t hal_can_REC(can_channel channel);//
void hal_can_irq_config(can_channel channel,irq_type irq,INTC_CPR_t priorty,can_enabled is_enable);
void hal_can_irq_enabled(can_channel channel,irq_type irq,can_enabled is_enable);

 void hal_busoff_Callback(CANDriver *drv) ;/* PRQA S 3673 */
void hal_can_init_test();
#endif /* HAL_CAN_H_ */
